/**
 * 
 */
package com.sso.sys.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池工具类
 * 
 * @author xdm
 */
public class ThreadPool {
	private static final int CORE_POOL_SIZE = 30;
	private static final int MAX_POOL_SIZE = 100;
	private static final int KEEP_ALIVE_TIME = 10;
	private static final int BLOCKING_QUEUE_SIZE = 10000;
	private static Object obj;
	/**
	 */
	private static ExecutorService payServer = new ThreadPoolExecutor(
			CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.MINUTES,
			new ArrayBlockingQueue<Runnable>(BLOCKING_QUEUE_SIZE),
			new ThreadPoolExecutor.DiscardOldestPolicy());
	

	/**
	 * 在线程池中运行Runnable类
	 * 
	 * @param runnable
	 */
	public static void payExecute(Runnable runnable) {
		if (payServer == null) {
			synchronized (obj) {
				payServer = new ThreadPoolExecutor(CORE_POOL_SIZE,
						MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.MINUTES,
						new ArrayBlockingQueue<Runnable>(BLOCKING_QUEUE_SIZE),
						new ThreadPoolExecutor.DiscardOldestPolicy());
			}
		}
		payServer.execute(runnable);
	}

	/**
     * 销毁方法
     */
    public static void destroy(){
        if(payServer != null){
        	payServer.shutdownNow();
        }
    }
}
